package com.cneiu.ppms.web.action;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.cneiu.ppms.web.base.BaseController;
import com.cneiu.ppms.web.model.DepartmentModel;
import com.cneiu.ppms.web.service.DepartmentService;
import com.jfinal.plugin.activerecord.Page;

/**
 * 部门的控制器Controller
 * @author zhaokf
 * @date 2013-04-15
 */
public class DepartmentController extends BaseController{
	
	/**
	 * 获取机构列表（ComboBox树形）
	 */
	public void treeComboBox(){
		/**根据前台传递过来的ID查询根节点**/
		String node = getPara("node");
		DepartmentModel model = DepartmentModel.dao.findFirst("select * from sa_dept where dept_no = ?", node);
		List<HashMap<Object,Object>> mapList =  null;
		/**可能存在多个根节点情况，获取每个根节点的子节点（递归）**/
		mapList = DepartmentService.treeComboBox(model);
		/**判断并返回数据**/
		if (null != mapList && mapList.size()>0){
			renderGrid(mapList);
		}else{
			renderAjax(false, DepartmentService.getError());
		}
	}
	
	
	
	/**
	 * 获取机构列表（树形）
	 */
	public void treeList(){
		/**根据前台传递过来的ID查询根节点**/
		String node = getPara("node");
		List<DepartmentModel> modelList = DepartmentModel.dao.find("select * from sa_dept where dept_no = ?", node);
		/**可能存在多个根节点情况，获取每个根节点的子节点（递归）**/
		List<HashMap<Object,Object>> mapList = new ArrayList<HashMap<Object,Object>>();
		for(DepartmentModel model:modelList){
			mapList.add(DepartmentService.treeList(model));
		}
		/**判断并返回数据**/
		if (null != mapList && mapList.size()>0){
			renderGrid(mapList);
		}else{
			renderAjax(false, DepartmentService.getError());
		}
	}

	/**
	 * 获取page limit得到部门列表
	 */
	public void list(){
		Page<DepartmentModel> Depts = DepartmentService.list(this.getParams());
		if (null != Depts){
			renderGrid(Depts.getList());
		}else{
			renderAjax(false, DepartmentService.getError());
		}
	}
     
	/**
	 * 添加部门信息
	 */
	public void add(){
		try{
			boolean isNotError = true;
			if (DepartmentService.isAdd(this.getParams())){
				isNotError = false;
			}
			renderAjax(isNotError, DepartmentService.getError());
		}
		catch (Exception e){
		}
	}

	/**
	 * 修改部门信息
	 */
	public void edit(){
		try{
			boolean isNotError = true;
			if (DepartmentService.isUpdate(this.getParams())){
				isNotError = false;
			}
			renderAjax(isNotError, DepartmentService.getError());
		}
		catch (Exception e){}
	}

	/**
	 * 删除部门数据
	 */
	public void delete(){
		try{
			boolean isNotError = true;
			if (DepartmentService.isDelete(this.getParams())){
				isNotError = false;
			}
			renderAjax(isNotError, DepartmentService.getError());
		}catch (Exception e){}
	}
}
